package com.zhipu.balance.ui.vm.home.system.personnel

import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.TimeUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.raizlabs.android.dbflow.annotation.Column
import com.zhipu.balance.listener.AddItemOnClickListener
import com.zhipu.balance.listener.AddOnClickListener
import com.zhipu.balance.listener.ChooseClickCallback
import com.zhipu.balance.mobile.dao.PersonnelDao
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.pop.AddPersonnelPopup
import com.zhipu.balance.pop.ModifyDeletePopup
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.system.personnel.PersonnelActivity
import com.zhipu.balance.ui.home.system.personnel.adapter.PersonnelAdapter
import com.zhipu.balance.ui.vm.BaseViewModel

class PersonnelViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService) , AddItemOnClickListener {
    /**
     * 适配器
     */
    var mPersonnelAdapter = PersonnelAdapter(this)

    /**
     * 人员
     */
    var mPersonnelList : List<Personnel>? = null

    init {
        PersonnelList()
    }



    fun PersonnelList(){
        mPersonnelList = PersonnelDao.getInstance().allList
        (mPersonnelList as MutableList<Personnel>?)?.let {
            mPersonnelAdapter.setInspectionPersonnel(
                it
            )
        }
    }





    fun AddPersonnel(){
        var mAddPersonnelPopup = AddPersonnelPopup(object : AddOnClickListener{
            override fun onSkipClicked(sKip: String?) {
                var mPersonnel = Personnel()
                /**
                 * 名称
                 */
                mPersonnel.name = sKip
                /**
                 * 创建时间
                 */
                mPersonnel.creationTime = TimeUtils.getNowMills()
                /**
                 * 添加
                 */
                if(mPersonnel.save()){
                    PersonnelList()
                }
            }
        })
        XPopup.Builder(ActivityUtils.getTopActivity())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .autoOpenSoftInput(false)
            .asCustom(mAddPersonnelPopup)
            .show();
    }

    /**
     * 关闭
     */
    fun shutDown(){
        ActivityUtils.finishActivity(PersonnelActivity::class.java)
    }


    /**
     * 点击
     */
    override fun onSkipClicked(position: Int, mObject: Any?) {
        var mPersonne : Personnel = mObject as Personnel
        var mModifyDeletePopup = ModifyDeletePopup(object : ChooseClickCallback{
            override fun onChooseClickCallback(mInt: Int) {
                when(mInt){
                    0 ->{
                        var mAddPersonnelPopup = AddPersonnelPopup(object : AddOnClickListener{
                            override fun onSkipClicked(sKip: String?) {
                                /**
                                 * 名称
                                 */
                                mPersonne.name = sKip
                                /**
                                 * 创建时间
                                 */
                                mPersonne.creationTime = TimeUtils.getNowMills()
                                /**
                                 * 添加
                                 */
                                if(mPersonne.update()){
                                    PersonnelList()
                                }
                            }
                        })
                        XPopup.Builder(ActivityUtils.getTopActivity())
                            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                            .autoOpenSoftInput(false)
                            .asCustom(mAddPersonnelPopup)
                            .show();
                    }
                    1 ->{
                        if(mPersonne.delete()){
                            PersonnelList()
                        }
                    }
                }
            }

        })
        XPopup.Builder(ActivityUtils.getTopActivity())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .autoOpenSoftInput(false)
            .asCustom(mModifyDeletePopup)
            .show();


    }

}